﻿using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.ComponentModel;
using System.Data;
using System.Collections.ObjectModel;

namespace DropDownFilterBlog
{
    [Guid("83f9fbbf-dba8-4a8e-a1f6-cafacbaafbbb")]
    public class DropDownFilterBlog : System.Web.UI.WebControls.WebParts.WebPart, ITransformableFilterValues
    {

        #region Private Variables

        private Label _textLabel = null;
        private DropDownList _sourceDropDownList = null;
        private SPList _sourceList = null;
        private string _sourceListName = null;
        private string _displayTextField = null;
        private string _valueField = null;

        #endregion

        #region Public Properties

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Text"),
        Category("Dropdown Filter"),
        WebDisplayName("Text")]
        public string TextLabel
        {
            get 
            {
                EnsureChildControls();
                return _textLabel.Text;
            }
            set
            {
                EnsureChildControls();
                _textLabel.Text = value;
            }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Source List Name"),
        Category("Dropdown Filter"),
        WebDisplayName("List Name")]
        public string SourceListName
        {
            get { return _sourceListName; }
            set { _sourceListName = value; }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Display Text Field"),
        Category("Dropdown Filter"),
        WebDisplayName("Display Text Field")]
        public string DisplayTextField
        {
            get { return _displayTextField; }
            set { _displayTextField = value; }
        }
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.User),
        WebDescription("Value Field"),
        Category("Dropdown Filter"),
        WebDisplayName("Value Field")]
        public string ValueField
        {
            get
            {
                string returnValue = String.Empty;

                if ((_valueField != null) && (_valueField.Length > 0))
                {
                    returnValue = _valueField;
                }
                else
                {
                    if (this.SourceList != null)
                    {
                        DataTable dataTable = this.SourceList.Items.GetDataTable();
                        if ((dataTable != null) && (dataTable.Columns.Count > 0))
                        {
                            returnValue = dataTable.Columns[0].ColumnName;
                        }
                    }
                }

                return returnValue;
            }
            set { _valueField = value; }
        }

        #endregion

        #region Private Properties

        private SPList SourceList
        {
            get
            {
                if ((_sourceList == null) && (this.SourceListName != null))
                {
                    try
                    {
                        _sourceList = SPContext.Current.Web.Lists[SourceListName];
                    }
                    catch
                    {
                        // TODO: Insert your custom error handling here
                    }
                }
                return _sourceList;
            }
        }

        #endregion

        #region Public Constructors

        public DropDownFilterBlog()
        {
        }

        #endregion

        #region Protected Methods

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            _textLabel = new Label();
            this.Controls.Add(_textLabel);

            _sourceDropDownList = new DropDownList();

            _sourceDropDownList.AutoPostBack = true;
            this.Controls.Add(_sourceDropDownList);

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadData();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
        }
        
        #endregion

        #region Private Methods

        private void LoadData()
        {
            if (this.Page.IsPostBack == false)
            {
                if ((SourceList != null) && (SourceList.Items != null))
                {
                    /*
                     * CBB 2008.10.08
                     * For some reason this method of binding SharePoint Lists
                     * to a control didn't work very well.  If the bound fields
                     * were Title or Name it worked well.  If it was some other
                     * field than it was not able to find the column.  Instead
                     * I've gone to a manual looping of the data (below)
                    _sourceDropDownList.DataSource = SourceList.Items;
                    if (this.DisplayTextField != null && this.DisplayTextField.Length > 0)
                    {
                        _sourceDropDownList.DataTextField = this.DisplayTextField;
                    }
                    if (this.ValueField != null && this.ValueField.Length > 0)
                    {
                        _sourceDropDownList.DataValueField = this.ValueField;
                    }
                    _sourceDropDownList.DataBind();
                    */
                    for (int i = 0; i < SourceList.Items.Count; i++)
                    {
                        SPListItem item = SourceList.Items[i];
                        ListItem listItem = new ListItem();

                        if (this.DisplayTextField != null && this.DisplayTextField.Length > 0)
                        {
                            listItem.Text = item[this.DisplayTextField].ToString();
                        }
                        else
                        {
                            listItem.Text = item[0].ToString();
                        }
                        if (this.ValueField != null && this.ValueField.Length > 0)
                        {
                            listItem.Value = item[this.ValueField].ToString();
                        }
                        else
                        {
                            listItem.Value = item[0].ToString();
                        }
                        _sourceDropDownList.Items.Add(listItem);
                    }
                }
            }
        }

        #endregion

        #region ITransformableFilterValues Members

        public bool AllowAllValue
        {
            get { return false; }
        }

        public bool AllowEmptyValue
        {
            get { return true; }
        }

        public bool AllowMultipleValues
        {
            get { return false; }
        }

        public string ParameterName
        {
            get
            {
                if ((this.ValueField != null) && (this.ValueField.Length > 0))
                {
                    return this.ValueField;
                }
                else
                {
                    return "Unknown";
                }

            }
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<string> ParameterValues
        {
            get
            {
                string selectedValue = String.Empty;

                if ((_sourceDropDownList!=null) && (_sourceDropDownList.SelectedItem != null))
                {
                    selectedValue = _sourceDropDownList.SelectedItem.Value;
                }


                string[] values = new string[] { selectedValue };
                return new ReadOnlyCollection<string>(values);
            }
        }

        #endregion

        #region Filter Provider Connection

        [ConnectionProvider("DropDown Filter", "ITransformableFilterValues", AllowsMultipleConnections = true)]
        public ITransformableFilterValues SetConnectionInterface()
        {
            return this;
        }

        #endregion


    }





}


